﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using GuRenFang.Service.Entity;
using GuRenFang.Service.IBiz;

namespace GuRenFang.Controllers.WebSite
{
    [ErrorHanderFilter]
    public class PictorialController : Controller
    {
        private readonly IPictorialCategoryBiz _pictorialCategoryService;

        public PictorialController(IPictorialCategoryBiz pictorialCategoryService)
        {
            _pictorialCategoryService = pictorialCategoryService;
        }

        public ActionResult Index(int? page = 1)
        {
            short size = Convert.ToInt16(ConfigurationManager.AppSettings["CategoryPhotoPageSize"]);

            int totalRecord = 0;
            IEnumerable<PictorialCategory> categorys = _pictorialCategoryService.GetCategoryByPages(out totalRecord,
                                                                                                    pageIndex:
                                                                                                        page.
                                                                                                        GetValueOrDefault
                                                                                                        (),
                                                                                                    pageSize: size,
                                                                                                    orderFiled: "");

            var item = new PageItem
                           {
                               CurrentPage = page.GetValueOrDefault(),
                               TotalRecords = totalRecord,
                               TotalPages = (totalRecord%size == 0) ? totalRecord/size : totalRecord/size + 1,
                               ControllerName = "Pictorial",
                               ActionName = "Index"
                           };

            var viewModels = new Pager<PictorialCategory> {DataSource = categorys, Item = item};

            return View(viewModels);
        }


        public ActionResult Category(int id, string name)
        {
            if (id <= 0 || string.IsNullOrEmpty(name))
            {
                return RedirectToAction("Index");
            }
            PictorialCategory category = _pictorialCategoryService.GetById(id);
            ViewBag.ThumPath = category.ThumbnailPath;
            string categoryName = Server.UrlDecode(name);
            string filePath = "~/UpFiles/写真/" + categoryName + "/";
            ViewBag.Category = categoryName;
            ViewBag.Path = filePath;
            List<string> model = Management.Common.GetImages(Server.MapPath(filePath));
            return View(model);
        }


        [ChildActionOnly]
        //[OutputCache(Duration = 3600)]
        public PartialViewResult _PhotoCategory(string photoType)
        {
            ViewBag.PhotoType = photoType;

            return PartialView("_PhotoCategory", _pictorialCategoryService.GetAll());
        }

       
    }
}
